package com.yejiali.backend.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @Author:yejiali
 * @Date:2025/7/15-07-15-15:12
 * @Decsription:com.yejiali.backend.util
 * @Version:1.0
 */
public class JsonUtil {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 将 JSON 字符串解析为指定类型的对象
     * @param json JSON 字符串
     * @param clazz 目标对象的类型
     * @param <T> 泛型类型
     * @return 解析后的对象
     * @throws JsonProcessingException 解析异常
     */
    public static <T> T parse(String json, Class<T> clazz) throws JsonProcessingException {
        return objectMapper.readValue(json, clazz);
    }

    /**
     * 将对象转换为 JSON 字符串
     * @param object 要转换的对象
     * @return JSON 字符串
     * @throws JsonProcessingException 转换异常
     */
    public static String toJson(Object object) throws JsonProcessingException {
        return objectMapper.writeValueAsString(object);
    }
}

